package com.android.lovingpet.mine.pet.view;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.base.TakePhotoFragment;
import com.android.lovingpet.bean.Pet;
import com.android.lovingpet.mine.pet.IPetContract;
import com.android.lovingpet.mine.pet.PetPresenter;
import com.android.lovingpet.util.MyPhotoWindow;
import com.android.lovingpet.util.Utility;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jph.takephoto.model.TResult;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ikata on 2018/5/2.
 */

public class EditPetFragment extends TakePhotoFragment<IPetContract.View, PetPresenter> implements IPetContract.View, View.OnClickListener {
    @BindView(R.id.civ_head)
    CircleImageView civHead;
    @BindView(R.id.bt_delete_pet)
    Button btDeletePet;
    Unbinder unbinder;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_birthday)
    TextView etBirthday;
    @BindView(R.id.sp_sterilize)
    Spinner etSterilize;
    @BindView(R.id.sp_sex)
    Spinner spSex;
    @BindView(R.id.sp_type)
    Spinner spType;
    private Pet pet = null;
    @Override
    public void next() {
        runOnUiThread(() -> getActivity().getSupportFragmentManager().popBackStackImmediate(null, 1));
    }

    @Override
    public void showMyPet(Pet pet) {

    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_edit_pet;
    }

    @Override
    protected void onCreateView(View view) {
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        setTitle(view,"编辑资料");
        initView();
    }

    private void initView() {
        Glide.with(getContext())
                .load(pet.getImage())
                .error(R.drawable.defaluthead)
                .into(civHead);
        etName.setText(pet.getName());
        spType.setSelection(Utility.matchArrayRes(R.array.petType, String.valueOf(pet.getType())));
        spSex.setSelection(Integer.parseInt(pet.getGender()));
        etBirthday.setText(pet.getBirthday());
    }

    @Override
    public PetPresenter createPresenter() {
        return new PetPresenter();
    }

    @Override
    public IPetContract.View createView() {
        return this;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.bt_delete_pet,R.id.civ_head})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_delete_pet:
                getPresenter().deletePet(pet.getId());
                break;
            case R.id.civ_head:
                new MyPhotoWindow(takePhoto,getView()).create();
                break;
        }
    }

    @Override
    public void takeSuccess(TResult result) {
      super.takeSuccess(result);
      Glide.with(getContext())
              .load(image)
              .error(R.drawable.defaluthead)
              .into(civHead);
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void getPet(Pet pet) {
        if (pet != null) {
            this.pet = pet;
        } else {
            this.pet = new Pet();
            Toast.makeText(getContext(), "数据获取失败!", Toast.LENGTH_LONG).show();
        }
    }
}
